package net.zhengxinyun.performance.App.service.unifyService;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.dto.report.ReportDTO;
import net.zhengxinyun.performance.App.dto.report.ReportDataDTO;
import net.zhengxinyun.performance.App.service.BaseIndexService.BaseService;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.CountryUnitIndicatorsEntity;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.mapper.Indicators.CountryIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.CountryUnitIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.report.IndexReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UnifyServiceImpl extends BaseService implements  UnifyService {

    @Autowired
    private IndexReportMapper indexReportMapper;

    @Autowired
    private TownIndicatorsEntityMapper townIndicatorsEntityMapper;
    @Autowired
    private CountryIndicatorsEntityMapper cityIndexMapper;
    @Autowired
    private CountryUnitIndicatorsEntityMapper deptIndexMapper;



    // 本月所有 指标报送情况
    @Override
    public List<ReportDTO> getReportData(ReportDTO dto) {
        int indexClass = dto.getIndexClass();
        List<ReportDTO> reportDTOS = new ArrayList<>();

        UserFilter uesr = getUserByTel(dto.getUserPhone().trim());
        IndexReportFilter reportFilter = new IndexReportFilter();

        reportFilter.setIndexClass(indexClass);
        reportFilter.setIndexDate(DateUtils.getLastMonth());

        switch (dto.getIndexClass() ){

            case 1:   //市对县

                CountryIndicatorsFilter cityFilter = new CountryIndicatorsFilter();
                cityFilter.setDateTime(dto.getDatetime());
                cityFilter.setAudit("1");
                cityFilter.setIndexType("二级指标");
                List<CountryIndicatorsFilter> query = cityIndexMapper.query(cityFilter);
                query = filterCityIndexs(query, uesr);

                for (CountryIndicatorsFilter item : query) {
                    reportFilter.setIndexId(item.getId());
                    List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
                    ReportDTO redto  = new ReportDTO();
                    redto.setIndexId(item.getId());
                    redto.setDatetime(reportFilter.getIndexDate());
                    redto.setIndexName(item.getIndexName());
                    if(ObjectUtils.isNotBlank(reportList))
                    {
                        redto.setReported(1);
                    }
                    reportDTOS.add(redto);
                }

                break;
            case 2:  //县对乡镇
                TownIndicatorsFilter townfilter = new TownIndicatorsFilter();
                townfilter.setIndexType("二级指标");
                townfilter.setDateTime(dto.getDatetime());
                List<TownIndicatorsFilter> listIndic = townIndicatorsEntityMapper.selectTownIndex(townfilter);
                listIndic=filterTownIndex(listIndic,uesr);

                for (TownIndicatorsFilter item : listIndic) {
                    reportFilter.setIndexId(item.getId());
                    List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
                    ReportDTO redto  = new ReportDTO();
                    redto.setIndexId(item.getId());
                    redto.setDatetime(reportFilter.getIndexDate());
                    redto.setIndexName(item.getIndexName());
                    if(ObjectUtils.isNotBlank(reportList))
                    {
                        redto.setReported(1);
                    }
                    reportDTOS.add(redto);

                }
                break;

            case 3: //县直单位
                CountryUnitIndicatorsFilter deptFilter = new CountryUnitIndicatorsFilter();
                deptFilter.setDateTime(dto.getDatetime());

                List<CountryUnitIndicatorsEntity> deptList = deptIndexMapper.query(deptFilter);
                deptList=filterDeptIndex(deptList,uesr);
                for (CountryUnitIndicatorsEntity item : deptList) {
                    reportFilter.setIndexId(item.getId());
                    List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
                    ReportDTO redto  = new ReportDTO();
                    redto.setIndexId(item.getId());
                    redto.setDatetime(reportFilter.getIndexDate());
                    redto.setIndexName(item.getIndexName());
                    if(ObjectUtils.isNotBlank(reportList))
                    {
                        redto.setReported(1);
                    }
                    reportDTOS.add(redto);
                }


                break;

        }


        return  reportDTOS;
    }

    List<IndexReportFilter>  getReports(IndexReportFilter reportFilter ){
        List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
        return reportList;
    }


    //单个指标报送情况
    @Override
    public List<ReportDataDTO> singleIndexReport(Long indexId,int indexClass) {
        List<ReportDataDTO> dataDto = new ArrayList<>();
        IndexReportFilter reportFilter = new IndexReportFilter();
        reportFilter.setIndexId(indexId);
        reportFilter.setIndexClass(indexClass);
        //reportFilter.setIndexDate(DateUtils.getLastMonth());
        List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);

        for (IndexReportFilter item : reportList) {

            ReportDataDTO dto  = new ReportDataDTO();
            dto.setDatetime(item.getIndexDate());
            dto.setReportTime(item.getIndextime());
            dto.setReporter(item.getUserName());
            dataDto.add(dto);

        }
        System.out.println(dataDto.size());
        String currentYear = getCurrentYear();
        TreeMap<String, String> yearmonth = Util.yearmonth(currentYear);

        List<ReportDataDTO> dataDto1 = new ArrayList<>();

        for (Map.Entry<String,String>  entry:yearmonth.entrySet() ){
            String key = entry.getKey();

            List<ReportDataDTO> templist = dataDto.stream().filter(reportDataDTO -> reportDataDTO.getDatetime().equals(key)).collect(Collectors.toList());
            if(ObjectUtils.isBlank(templist)){
                ReportDataDTO rdd = new ReportDataDTO();
                rdd.setReported(0);
                rdd.setReportTime("-");
                rdd.setReporter("-");
                rdd.setDatetime(key);


                dataDto1.add( rdd);

            }else {
                ReportDataDTO dto = templist.get(0);
                dto.setReported(1);
                dataDto1.add(dto);
            }

        }


        return  dataDto1;
    }
}
